<?php

namespace app\admin\controller;

use app\admin\model\Manager as ManagerModel;
use app\admin\model\TeacherModel;
use app\admin\model\User as UserModel;
use app\BaseController;

class User extends BaseController
{
    /* 列表 */
    public function index()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $page = input('page', 1, 'intval');
        $perpage = 8;
        $result = UserModel::order('id desc')->withCount([
            'paper'
        ])->page($page, $perpage)->select();
        $total = UserModel::count();
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $perpage,
            'data' =>  $result
        ];
        return show(config('status.success'), '会员列表', $data);
    }

    public function getMyInfo()
    {
        $user = TeacherModel::where([
            'id' => request()->userinfo['id'],
        ])->find();
        return show(config('status.success'), 'success', $user);
    }

    /**
     * 获取用户信息
     * @return array|mixed
     */
    public function getpwd()
    {
        $id = input('id', '');

        $result = ManagerModel::where(['id' => $id])->find();

        return $result;
    }

    /**
     * 修改总后台密码
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function uppwd()
    {
        $id = input('id', '');
        $password = input('password', '');
        $result = ManagerModel::where(['id' => $id])->find();
        $dbPwd = md5(md5($password) . $result['extra']);
        $data = ManagerModel::where(['id' => $id])->update(['password'=>$dbPwd]);
        return show(config('status.success'),  'success');
//        849b556d3c96dc6b3da100a95e57cd63
    }

    public function updatePwd()
    {
        $data = [
            'phone' => request()->post('phone'),
        ];
        $pwd = request()->post('new_pwd');
        if (!empty($pwd)) {
            $data['password'] = sha1(md5($pwd));
        }
        TeacherModel::where([
            'id' => request()->userinfo['id'],
        ])->update($data);

        return show(config('status.success'), 'success');
    }

    /* 搜索 */
    public function search()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $text = input('text', '');
        $page = input('page', 1, 'intval');
        $perpage = 8;
        $result = UserModel::order('id desc')->page($page, $perpage)->whereLike('wx|name|tel|address', '%' . $text . '%')->select();
        $total = UserModel::whereLike('wx|name|tel|address', '%' . $text . '%')->count();
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $perpage,
            'data' =>  $result
        ];
        return show(config('status.success'), '会员列表', $data);
    }
}
